#region Using Directives

using System;
using System.Web.UI;
using Eneta.Portal.Forum.Controls.Base;
using Eneta.Portal.Forum.Controls.Common;
using Eneta.Portal.Forum.Core.Domain.Entities;
using Eneta.Portal.Forum.Core.Service.Application;

#endregion

namespace Eneta.Portal.Forum.Controls
{
    /// <summary>
    /// Summary description for Topics.
    /// </summary>
    public class ViewTopics : BaseForumControl
    {
        /// <summary>
        /// Notifies server controls that use composition-based implementation to create any child
        /// controls they contain in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            Controls.Add(BuildBasePageLinks());
            Controls.Add(new LiteralControl("<br>"));

            Controls.Add(new LiteralControl("<table width=100% cellspacing=0 cellpadding=3>"));
            Controls.Add(new LiteralControl("<tr>"));
            Controls.Add(new LiteralControl("<td align=left>&nbsp;</td>"));
            Controls.Add(new LiteralControl("<td align=right>"));

            var forum = RepositoryRegistry.ForumRepository.GetById(_forumID);

            if(ForumApplication.Instance.CurrentUser != null)
                if (forum.HasAccess(ForumApplication.Instance.CurrentUser, Permission.Rights.Add))
                {
                    Controls.Add(new LiteralControl(
                                     String.Format("<a href=\"{0}\">{1}</a>",
                                                   ForumApplication.Instance.GetNewTopicLink(forum.Id, PostMode.New.ToString()),
                                                   Resources.UI.TextNewTopic)
                                     ));
                }

            Controls.Add(new LiteralControl("</td>"));
            Controls.Add(new LiteralControl("</tr>"));
            Controls.Add(new LiteralControl("</table>"));

            Controls.Add(new LiteralControl("<table width=100% cellspacing=1 cellpadding=3>"));
            Controls.Add(new LiteralControl("<tr>"));
            Controls.Add(new LiteralControl(string.Format("<td colspan=6 class=\"ms-ToolPaneTitle\">{0}</td>", _forumName)));
            Controls.Add(new LiteralControl("</tr>"));
            Controls.Add(new LiteralControl("<tr>"));
            Controls.Add(new LiteralControl("<td class=\"ms-TPHeader\" width=1%>&nbsp;</td>"));
            Controls.Add(new LiteralControl(String.Format("<td class=\"ms-TPHeader\" align=left><strong>{0}</strong></td>", 
                                                          Resources.UI.TextTopics)));
            Controls.Add(new LiteralControl(String.Format("<td class=\"ms-TPHeader\" align=middle width=7%><strong>{0}</strong></td>",
                                                          Resources.UI.TextReplies)));
            Controls.Add(new LiteralControl(String.Format("<td class=\"ms-TPHeader\" align=middle width=20%><strong>{0}</strong></td>",
                                                          Resources.UI.TextAuthor)));
            Controls.Add(new LiteralControl(String.Format("<td class=\"ms-TPHeader\" align=middle width=7%><strong>{0}</strong></td>",
                                                          Resources.UI.TextViews)));
            Controls.Add(new LiteralControl(String.Format("<td class=\"ms-TPHeader\" align=middle width=25%><strong>{0}</strong></td>",
                                                          Resources.UI.TextLastPost)));
            Controls.Add(new LiteralControl("</tr>"));

            DisplayTopics(RepositoryRegistry.TopicRepository.FindByForumId(_forumID));

            DisplayFooterSectionWithRssFeed();

            Controls.Add(new LiteralControl("</table>"));
        }

        private void DisplayFooterSectionWithRssFeed()
        {
            Controls.Add(new LiteralControl("<tr>"));
            Controls.Add(new LiteralControl("<td colspan=\"6\" align=\"right\">"));
            var feedLink = ForumApplication.Instance.GetLink(SharePointForumControls.SynFeed, "forum={0}", _forumID);
            Controls.Add(new LiteralControl(string.Format("<a href=\"{0}\">{1}</a>", feedLink, Resources.UI.TextRSS)));
            Controls.Add(new LiteralControl("</td>"));
            Controls.Add(new LiteralControl("</tr>"));
        }
    }
}